package com.definesys.smecrailtransit.api;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleLineBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStation;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

import static com.definesys.smecrailtransit.SmecApplication.getContext;

/**
 * Created by xupeizuo on 2017/8/30.
 */

public final class SmecRailConstant implements Serializable{

    /**
     * 导航标题
     */
    private static HashMap<Integer,Integer> navigationTitle;

    /**
     * 电梯标题
     */
    private static ArrayList<String> titleArrayList;

    /**
     * 下拉刷新头部
     */
    private static SinaRefreshView sinaRefreshView;

    /**
     * 线路站点信息
     */

    private static ArrayList<String> lineBeen=new ArrayList<>();

    private static ArrayList<ArrayList<String>> arrayLists=new ArrayList<>();

    private static HashMap<String,ArrayList<EleStation>> map=new HashMap<>();



    private static SlideInLeftAnimator slideInLeftAnimator;

    private static SmecRailConstant smecRailConstant;
    private static byte[] syncObj=new byte[0];

    public static SmecRailConstant initConstant(){
        if(smecRailConstant == null){
            synchronized (syncObj){
                if(smecRailConstant == null){
                    smecRailConstant=new SmecRailConstant();
                    _initNavigationTitle();
                    _initTitle();
                }
            }
        }
        return smecRailConstant;
    }

    private static void _initNavigationTitle(){
        navigationTitle=new HashMap<>();
        navigationTitle.put(0, R.string.real_time);
        navigationTitle.put(1,R.string.site_details);
        navigationTitle.put(2,R.string.real_status);
        navigationTitle.put(3,R.string.past_report);
        navigationTitle.put(4,R.string.operator_status);
    }

    private static void _initTitle(){
        titleArrayList=new ArrayList<>();
        titleArrayList.add("电梯实时状态");
        titleArrayList.add("电梯异常统计");
        titleArrayList.add("电梯运行时长");
    }

    /**
     * 获取今天详细时间
     * @return
     */
    public static String getNowTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return sdf.format(new Date());
    }

    /**
     * 获取今天是几号
     * @return
     */
    public static SimpleDateFormat getToday(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf;
    }

    /**
     * 获取是上下午
     * @return
     */
    public static String getAmOrPm(){
        long time = System.currentTimeMillis();
        final Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int apm = mCalendar.get(Calendar.AM_PM);
        if(apm == 0){
            return "上午好";
        }
        if(apm == 1){
            return "下午好";
        }
        return "";
    }

    /**
     * 获取今天是星期几
     * @return
     */
    public static String getWeek(){
        String week="";
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(getToday().parse(getToday().format(new Date())));
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            week += "星期天";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            week += "星期一";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            week += "星期二";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            week += "星期三";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            week += "星期四";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            week += "星期五";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            week += "星期六";
        }
        return week;
    }

    public static SlideInLeftAnimator getSlideInLeftAnimator(){
        if(slideInLeftAnimator ==null){
            slideInLeftAnimator=new SlideInLeftAnimator();
            slideInLeftAnimator.setAddDuration(300);
        }
        return slideInLeftAnimator;
    }

    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className)) {
            return false;
        }

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }

        return false;
    }

    public static HashMap<Integer,Integer> getNavigationTitle(){
        return navigationTitle;
    }

    public static ArrayList<String> getTitleArrayList(){
        return titleArrayList;
    }

    public static void setEleLineBeanArrayList(ArrayList<EleLineBean> arrayList) {

        if(CommonUtils.notEmpty(arrayList)){
            for(int i =0 ;i<arrayList.size();i++){
                EleLineBean eleLineBean=arrayList.get(i);
                lineBeen.add(eleLineBean.getLineName());
                ArrayList<String> strings=new ArrayList<>();
                for(int j=0;j<eleLineBean.getLineStations().size();j++){
                    strings.add(eleLineBean.getLineStations().get(j).getStationName());
                }
                arrayLists.add(strings);
                map.put(eleLineBean.getLineName(),eleLineBean.getLineStations());
            }
        }
    }

    public static ArrayList<ArrayList<String>> getArrayLists() {
        return arrayLists;
    }

    public static ArrayList<String> getLineBeen() {
        return lineBeen;
    }

    public static HashMap<String, ArrayList<EleStation>> getMap() {
        return map;
    }

    public static SinaRefreshView getSinaRefreshView() {
        sinaRefreshView=new SinaRefreshView(getContext());
        sinaRefreshView.setArrowResource(R.drawable.smec_refresh);
        sinaRefreshView.setTextColor(getContext().getResources().getColor(R.color.customWhite));
        return sinaRefreshView;
    }
}
